package com.ruoyi.web.controller.merchantStatistics;

import com.ruoyi.basedata.domain.PayKind;
import com.ruoyi.basedata.service.IPayKindService;
import com.ruoyi.channel.domain.MpChannel;
import com.ruoyi.channel.service.IMpChannelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.merchant.domain.MpMerchant;
import com.ruoyi.merchant.domain.MpMerchantRSP;
import com.ruoyi.merchant.service.IMpMerchantService;
import com.ruoyi.merchantStatistics.domain.MerchantStatisticsExport;
import com.ruoyi.merchantStatistics.domain.MerchantStatisticsPay;
import com.ruoyi.merchantStatistics.service.IMerchantStatisticsPayService;
import com.ruoyi.statistics.domain.StatisticsPay;
import com.ruoyi.statistics.domain.StatisticsPayExport;
import com.ruoyi.statistics.service.IStatisticsPayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-07
 */
@Controller
@RequestMapping("/merchantStatistics/statisticsPay")
public class MerchantStatisticsPayController extends BaseController
{
    private String prefix = "merchantStatistics/statisticsPay";
	
	@Autowired
	private IMerchantStatisticsPayService statisticsPayService;
	@Autowired
	private IMpMerchantService mpMerchantService;
	@Autowired
	private IMpChannelService channelService;
	@Autowired
	private IPayKindService payKindService;

	
	@RequiresPermissions("merchantStatistics:statisticsPay:view")
	@GetMapping()
	public String statisticsPay()
	{
	    return prefix + "/statisticsPay";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("merchantStatistics:statisticsPay:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MerchantStatisticsPay statisticsPay)
	{
		MpMerchant mpMerchant = new MpMerchant();
		mpMerchant.setUserId(ShiroUtils.getUserId().intValue());
		List<MpMerchantRSP> mpMerchantRSPS = mpMerchantService.selectMpMerchantList(mpMerchant);
		if(mpMerchantRSPS.size() > 0){
			MpMerchantRSP mpMerchantRSP = mpMerchantRSPS.get(0);
			statisticsPay.setMerchantId(mpMerchantRSP.getId());
		}else {
			statisticsPay.setMerchantId(0L);
		}
		if (StringUtils.isNull(statisticsPay.getChannelName())) {
			statisticsPay.setChannelName("");
		}
		if (StringUtils.isNull(statisticsPay.getPayKindName())) {
			statisticsPay.setPayKindName("");
		}
		startPage();
        List<MerchantStatisticsPay> list = statisticsPayService.selectStatisticsPayList(statisticsPay);
		return getDataTable(list);
	}

	/**
	 * 查询渠道列表
	 */
	@PostMapping("/channelList")
	@ResponseBody
	public TableDataInfo list(MpChannel mpChannel)
	{
		startPage();
		List<MpChannel> list = channelService.selectMpChannelList(mpChannel);
		return getDataTable(list);
	}


	/**
	 * 查询支付类型列表
	 */
	@PostMapping("/paykindList")
	@ResponseBody
	public TableDataInfo list(PayKind payKind)
	{
		startPage();
		List<PayKind> list = payKindService.selectPayKindList(payKind);
		return getDataTable(list);
	}



	/**
	 * 导出列表
	 */
	@RequiresPermissions("merchantStatistics:statisticsPay:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MerchantStatisticsPay statisticsPay)
    {
		MpMerchant mpMerchant = new MpMerchant();
		mpMerchant.setUserId(ShiroUtils.getUserId().intValue());
		List<MpMerchantRSP> mpMerchantRSPS = mpMerchantService.selectMpMerchantList(mpMerchant);
		if(mpMerchantRSPS.size() > 0){
			MpMerchantRSP mpMerchantRSP = mpMerchantRSPS.get(0);
			statisticsPay.setMerchantId(mpMerchantRSP.getId());
		}else {
			statisticsPay.setMerchantId(0L);
		}
    	List<MerchantStatisticsPay> list = statisticsPayService.selectStatisticsPayList(statisticsPay);
		List<MerchantStatisticsExport> merchantStatisticsExportList = dataConvert(list);
        ExcelUtil<MerchantStatisticsExport> util = new ExcelUtil<MerchantStatisticsExport>(MerchantStatisticsExport.class);
        return util.exportExcel(merchantStatisticsExportList, "statisticsPay");
    }


	/**
	 * 数据转化
	 */
	public  List<MerchantStatisticsExport> dataConvert(List<MerchantStatisticsPay> list) {
		List<MerchantStatisticsExport> statisticsPayExports = new ArrayList<>();
		for (MerchantStatisticsPay statisticsPay : list) {
			MerchantStatisticsExport statisticsPayExport = new MerchantStatisticsExport();
			BeanUtils.copyProperties(statisticsPay, statisticsPayExport);
			BigDecimal yuan = new BigDecimal("100");
			// 预期支付额
			statisticsPayExport.setExpectFee(statisticsPay.getExpectFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));

			// 预期商户入账
			statisticsPayExport.setExpectMchFee(statisticsPay.getExpectMchFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));

			// 实际支付额
			statisticsPayExport.setRealFee(statisticsPay.getRealFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));

			// 实际商户入账
			statisticsPayExport.setRealMchFee(statisticsPay.getRealMchFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));


			// 统计类型
			if (statisticsPay.getStatisticsType() == 1) {
				statisticsPayExport.setType("天");
			}else if (statisticsPay.getStatisticsType() == 2) {
				statisticsPayExport.setType("小时");
			}
			statisticsPayExports.add(statisticsPayExport);
		}
		return statisticsPayExports;
	}



	/**
	 * 新增
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@RequiresPermissions("merchantStatistics:statisticsPay:add")
	@Log(title = "", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(MerchantStatisticsPay statisticsPay)
	{		
		return toAjax(statisticsPayService.insertStatisticsPay(statisticsPay));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		MerchantStatisticsPay statisticsPay = statisticsPayService.selectStatisticsPayById(id);
		mmap.put("statisticsPay", statisticsPay);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@RequiresPermissions("merchantStatistics:statisticsPay:edit")
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MerchantStatisticsPay statisticsPay)
	{		
		return toAjax(statisticsPayService.updateStatisticsPay(statisticsPay));
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("merchantStatistics:statisticsPay:remove")
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(statisticsPayService.deleteStatisticsPayByIds(ids));
	}
	
}
